package cn.iocoder.yudao.module.biz.controller.admin.operationTicket;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.operationTicket.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.operationTicket.BizOperationTicketDO;
import cn.iocoder.yudao.module.biz.convert.operationTicket.BizOperationTicketConvert;
import cn.iocoder.yudao.module.biz.service.operationTicket.BizOperationTicketService;

@Api(tags = "管理后台 - 操作票")
@RestController
@RequestMapping("/biz/operation-ticket")
@Validated
public class BizOperationTicketController {

    @Resource
    private BizOperationTicketService operationTicketService;

    @PostMapping("/create")
    @ApiOperation("创建操作票")
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:create')")
    public CommonResult<String> createOperationTicket(@Valid @RequestBody BizOperationTicketCreateReqVO createReqVO) {
        return success(operationTicketService.createOperationTicket(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新操作票")
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:update')")
    public CommonResult<Boolean> updateOperationTicket(@Valid @RequestBody BizOperationTicketUpdateReqVO updateReqVO) {
        operationTicketService.updateOperationTicket(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除操作票")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:delete')")
    public CommonResult<Boolean> deleteOperationTicket(@RequestParam("id") String id) {
        operationTicketService.deleteOperationTicket(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得操作票")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:query')")
    public CommonResult<BizOperationTicketRespVO> getOperationTicket(@RequestParam("id") String id) {
        BizOperationTicketDO operationTicket = operationTicketService.getOperationTicket(id);
        return success(BizOperationTicketConvert.INSTANCE.convert(operationTicket));
    }

    @GetMapping("/list")
    @ApiOperation("获得操作票列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:query')")
    public CommonResult<List<BizOperationTicketRespVO>> getOperationTicketList(@RequestParam("ids") Collection<String> ids) {
        List<BizOperationTicketDO> list = operationTicketService.getOperationTicketList(ids);
        return success(BizOperationTicketConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得操作票分页")
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:query')")
    public CommonResult<PageResult<BizOperationTicketRespVO>> getOperationTicketPage(@Valid BizOperationTicketPageReqVO pageVO) {
        PageResult<BizOperationTicketDO> pageResult = operationTicketService.getOperationTicketPage(pageVO);
        return success(BizOperationTicketConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出操作票 Excel")
    @PreAuthorize("@ss.hasPermission('biz:operation-ticket:export')")
    @OperateLog(type = EXPORT)
    public void exportOperationTicketExcel(@Valid BizOperationTicketExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizOperationTicketDO> list = operationTicketService.getOperationTicketList(exportReqVO);
        // 导出 Excel
        List<BizOperationTicketExcelVO> datas = BizOperationTicketConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "操作票.xls", "数据", BizOperationTicketExcelVO.class, datas);
    }

}
